package com.ws.controller;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import javax.print.attribute.HashAttributeSet;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ws.config.EasyWebConfig;
import com.ws.util.UploadFileUtil;
import com.ws.bean.Staff;
import com.ws.common.CommonResult;
import com.ws.service.StaffService;
import com.ws.util.Page;

@RestController
@RequestMapping("staff")
public class StaffController {
	@Autowired
	StaffService service;
	
	@PostMapping("upload")
	public CommonResult staffImg(@RequestParam("file") MultipartFile file,Integer id) {
		System.out.println(file);
		System.out.println("file");
		//存储文件
		String newName = UploadFileUtil.uploadFile(file);
		//将文件地址存入数据库
		//返回上传文件的地址  返回上传头像的员工数据
		
		return CommonResult.success(EasyWebConfig.STATIC_PATH + newName);
	}
	@PutMapping("staffimg")
	public CommonResult staffimg(Staff staff) {
		int result = service.addImg(staff);
		return CommonResult.success();
	}
		@GetMapping("staff")
		public CommonResult getAllStaff(Staff staff, Page page) {
			List<Staff> list = service.getAllStaff(staff,page);
			int count = service.getCount(staff);
			Map map = new HashMap<>();
			map.put("list", list);
			map.put("count", count);
			return CommonResult.success(map);
}		
		@PutMapping("edit")
		public CommonResult editStaff(Staff staff) {
			int result = service.editStaff(staff);
			return CommonResult.success(result);
		}
		
		@DeleteMapping("del/{id}")
		public CommonResult delStaff(@PathVariable Integer id) {
			int result = service.delStaff(id);
			return CommonResult.success(result);
		}
		@PostMapping("add")
		public CommonResult addStaff(@RequestBody Staff staff) {
			int result = service.addStaff(staff);
			return CommonResult.success(result);
		}
		
}
